<?php

function postRequest($url,$data=null,$cookie=null,$gbk=false)
{
    if (is_array($data))
    {
       ksort($data);
       $data = http_build_query($data);
    }
    if(is_array($cookie))
    {
        $str = '';
        foreach ($cookie as $key => $value){
            $str .= $key.'='.$value.'; ';
        }
        $cookie = substr($str,0,-1);
     }
    $opts = array(
       'http'=>array(
       'method'=>"POST",
       'header'=>"Content-type: application/x-www-form-urlencoded\r\n".
                  "Content-length:".strlen($data)."\r\n" . 
                  "Cookie: ".$cookie."\r\n" . 
                  "\r\n",
       'content' => $data,
       )
    );
    $context = stream_context_create($opts);
    $ret = file_get_contents($url, false, $context);
	if ($gbk) $ret = mb_convert_encoding($ret, "UTF-8", "GBK");
    return $ret;
}

function join_cookie($cook)
{
    $d = Array();
    foreach( $cook as $k=>$v )
    {
		$d[] =$k."=".$v;
    }
    $data = implode(";",$d);
	return $data;
}

function join_post($cook)
{
 $fields_string='';
 foreach($cook as $key=>$value) { $fields_string .= urlencode($key).'='.$value.'&' ; }
 rtrim($fields_string ,'&') ;

 return $fields_string;
}

function catcha($url,$putout=1,&$post_data=array(),$cook=array(),$gbk=false)
{
		if ($putout) echo "\n<p/>";
		
		$ch = curl_init();
		// 设置 url
		curl_setopt($ch, CURLOPT_URL, $url);
		

		
		//cookie data
		curl_setopt($ch, CURLOPT_COOKIE, join_cookie($cook));
        
        // 页面内容我们并不需要
        curl_setopt($ch, CURLOPT_NOBODY, 0);
        // 返回HTTP header
        curl_setopt($ch, CURLOPT_HEADER, 1);
        // 返回结果，而不是输出它
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		
		//post data
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_POSTFIELDS,$post_data);
		
		//重定向
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
		//执行！
        $output = curl_exec($ch);
		curl_close($ch);
        // 有重定向的HTTP头信息吗?
        if (preg_match("!Location: (.*)!", $output, $matches)) {
            //echo "redirects to $matches[1]\n<br>";

        } else {
            //echo "no redirection\n<br>";
        }
		
		if (preg_match('|id="__VIEWSTATE" value="(.*?)" />|', $output, $matches)) {
            $post_data['__VIEWSTATE']=$matches[1];

        } else {
            $post_data['__VIEWSTATE']=null;
        }
		
		if (preg_match('|id="__EVENTVALIDATION" value="(.*?)" />|', $output, $matches)) {
            $post_data['__EVENTVALIDATION']=$matches[1];

        } else {
           $post_data['__EVENTVALIDATION']=null;
        }

		
		if ($gbk) $result = mb_convert_encoding($output, "UTF-8", "GBK");
		else $result=$output;
		
		//echo join_cookie($cook);
		if ($putout) echo "$result\n<br>";
		return $result;
}

function get_client_ip(){
   if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown"))
       $ip = getenv("HTTP_CLIENT_IP");
   else if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown"))
       $ip = getenv("HTTP_X_FORWARDED_FOR");
   else if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown"))
       $ip = getenv("REMOTE_ADDR");
   else if (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown"))
       $ip = $_SERVER['REMOTE_ADDR'];
   else
       $ip = "unknown";
   return($ip);
}


// 格式化时间
function format_time( $time, $mode = 0 ) {
	date_default_timezone_set ( "ASIA/SHANGHAI" ); 
	if ($mode)
	{
		$today = date("Y-m-d", time() );
		$date = date("Y-m-d", $time);
		if ($date != $today) return $date; 
			else return date("H:i:s", $time);
	}
	else
	{
		return date("Y-m-d H:i:s", $time);
	}
}

// 格式化字符串
function format_text( $str ) {
	import("ORG.Util.Input");
	return Input::forShow($str);
}

function get_time_stamp(){
	return time();
}
